package ChatApp.ServerPart;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Zuxs
 * Date: 4.09.12
 * Time: 23:50
 * To change this template use File | Settings | File Templates.
 */
public class User extends Thread {

    public ObjectInputStream objectInputStream;
    public ObjectOutputStream objectOutputStream;
    private boolean isSynchronized = false;
    public UsersCeeper usersCeeper;
    public Socket socket;
    public String name;


    public User(Socket socket) {
        this.socket = socket;
        try {
            this.objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            this.objectInputStream = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if (objectInputStream != null && objectOutputStream != null) {
            run();
        }
    }

    @Override
    public void run() {
//        this.SyncronizeLists(usersCeeper.fileWithUsersAtLocalMachine());
//        try {
//            while (true) {
//                //wait until triger
//                wait();
//
//                Object o = objectInputStream.readObject();
//                if (o instanceof File) {
//                    File tmp = (File) o;
//                    SyncronizeLists(tmp);
//
//                }
//
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        } catch (IOException e) {
//            connectionLost();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
    }

    @Override
    public boolean equals(Object pO) {
        User u = (User) pO;
        String tmpHostThis = this.socket.getInetAddress().getHostAddress();
        String tmpHostUser =  u.socket.getInetAddress().getHostAddress();
        if (tmpHostThis.equals(tmpHostUser))
            if (u.name.equals(this.name))
                return true;

        return false;
    }

    @Override
    public String toString(){
       String host = socket.getInetAddress().getHostAddress();
       int port = socket.getPort();
       return this.name +","+host+","+port;
    }

    public void SendMessage(String msg) {

        try {
            objectOutputStream.writeObject(msg);
            objectOutputStream.flush();
        } catch (IOException e) {
            connectionLost();
        }

    }

//    public File SyncronizeLists(File f) {
//        if (!isSynchronized)
//            try {
//                //add boolean is Synchronized
//
//                //Creating file to send
//                UsersCeeper uc = new UsersCeeper();
//                uc.readUserFromFile(f);
//                f = uc.fileWithUsersAtLocalMachine();
//                Object o = (Object) f;
//                // file created
//
//                //send file
//                objectOutputStream.writeObject(o);
//                objectOutputStream.flush();
//                //file sent
//
//                //create request string
//                String requestString = "Synchronized:" + this.name;
//                objectOutputStream.writeObject(requestString);
//                objectOutputStream.flush();
//                // waiting answer
//
//                o = objectInputStream.readObject();
//                if (o instanceof File) {
//                    isSynchronized = true;
//                    return (File) o;
//                } else
//                    return null;
//            } catch (IOException e) {
//                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//            }
//
//        return null;
//    }
//
//    public void setCurrentList(ArrayList<User> al) {
//        //       usersCeeper. = al;
//    }
//
    private void connectionLost() {
         usersCeeper.remove(this);
               try {
                   this.finalize();
               } catch (Throwable throwable) {
                   throwable.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
               }
    }

}
